﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

public class FM_Pause : FM_BaseNote
{
    private int adjustment = 1;

    public FM_Pause(FM_ScoreBase Score, FM_DurationValue duration, int adjustment, int voice) : base(FM_NoteType.PAUSE, Score)
    {
        this.duration = duration;
        this.adjustment = adjustment;
        this.voice = voice;
    }

    public FM_Pause(FM_ScoreBase Score, FM_DurationValue duration, int adjustment, int voice, Color Color) : base(FM_NoteType.PAUSE, Score)
    {
        this.duration = duration;
        this.color = Color;
        this.adjustment = adjustment;
        this.voice = voice;
    }

    public override float GetDisplacement()
    {
        int durationInt = (int)duration;

        if (durationInt == 1 || durationInt == 51) return -adjustment + 1.0f;
        else
        {
            if (durationInt == 2 || durationInt == 52) return -adjustment + 2.0f;
            else
            {
                if (durationInt == 4 || durationInt == 54) return -adjustment + 2.0f;
                else
                {
                    if (durationInt == 8 || durationInt == 58) return -adjustment + 2.0f;
                    else
                    {
                        if (durationInt == 16 || durationInt == 66) return -adjustment + 1.0f;
                        else
                        {
                            if (durationInt == 32 || durationInt == 82) return -adjustment + 2.0f;
                        }
                    }
                }
            }
        }
        return -adjustment + 2.0f;
    }

    public override string AsString()
    {
        int durationInt = (int)duration;
        if (durationInt == 1 || durationInt == 51) return FM_Const.Pause_1;
        if (durationInt == 2 || durationInt == 52) return FM_Const.Pause_2;
        if (durationInt == 4 || durationInt == 54) return FM_Const.Pause_4;
        if (durationInt == 8 || durationInt == 58) return FM_Const.Pause_8;
        if (durationInt == 16 || durationInt == 66) return FM_Const.Pause_16;
        if (durationInt == 32 || durationInt == 82) return FM_Const.Pause_32;
        return string.Empty;
    }

    int LineSpan()
    {
        int durationInt = (int)duration;
        if (durationInt == 1 || durationInt == 51) return 1;
        if (durationInt == 2 || durationInt == 52) return 1;
        if (durationInt == 4 || durationInt == 54) return 3;
        if (durationInt == 8 || durationInt == 58) return 2;
        if (durationInt == 16 || durationInt == 66) return 3;
        if (durationInt == 32 || durationInt == 82) return 4;
        return 0;
    }

    string AsStringDot()
    {
        if ((int)duration > 50) return " " + FM_Const.Dot;
        return string.Empty;
    }

    public override float WidthAccidental()
    {
        return 0;
    }

    public override float WidthNoteNoStem()
    {
        if (score.score == null) return 0;
        FM_Const.AdjustFont(score.score, FM_Const.Pause_8, 2);
        return score.score.Font.MeasureText(AsString());
    }

    public override float WidthNote()
    {
        return WidthNoteNoStem();
    }

    public override float WidthDot()
    {
        if (score.score == null) return 0;
        FM_Const.AdjustFont(score.score, FM_Const.Pause_8, 2);
        return score.score.Font.MeasureText(AsStringDot());
    }

    private float BottomMargin()
    {
        if (score.score == null) return 0;
        FM_Const.AdjustFont(score.score, AsString(), LineSpan());
        Rect bounds = new Rect();
        string s = AsString();
        score.score.Font.GetTextBounds(s, 0, s.Length, bounds);
        return bounds.bottom;//.Bottom;
    }

    private float TopMargin()
    {
        if (score.score == null) return 0;
        FM_Const.AdjustFont(score.score, AsString(), LineSpan());
        Rect bounds = new Rect();
        string s = AsString();
        score.score.Font.GetTextBounds(s, 0, s.Length, bounds);
        return bounds.top;//.Top;
    }

    public override void DrawNote(ScoreCanvas canvas)
    {
        if (score.score == null) return;
        if (!IsBlurred() && !IsVisible()) return;
        base.DrawNote(canvas);
        //if (IsBlurred())
        //    score.score.Font.MaskFilter = new BlurMaskFilter(30, BlurMaskFilter.Blur.Normal);
        //else score.score.Font.MaskFilter = null;
        FM_Const.AdjustFont(score.score, FM_Const.Pause_8, 2);
        canvas.DrawText(AsString(), StartX + paddingLeft + WidthAccidental() + paddingNote, StartY1 + GetDisplacement() * score.score.GetDistanceBetweenStaveLines(), score.score.Font);
        score.score.Font.Color = score.score.GetColor();
        canvas.DrawText(AsStringDot(), StartX + paddingLeft + WidthAccidental() + paddingNote + WidthNote() + paddingDot, StartY1 + (GetDisplacement() + 0.5f) * score.score.GetDistanceBetweenStaveLines(), score.score.Font);
        //score.score.Font.MaskFilter = null;
    }

    public override float Left()
    {
        return StartX + paddingLeft;
    }

    public override float Bottom()
    {
        if (score.score == null) return 0;
        return StartY1 + GetDisplacement() * score.score.GetDistanceBetweenStaveLines() + BottomMargin();
    }

    public override float Right()
    {
        if (score.score == null) return 0;
        return StartX + paddingLeft + WidthAccidental() + paddingNote + WidthNote() + paddingDot + WidthDot();
    }

    public override float Top()
    {
        if (score.score == null) return 0;
        return StartY1 + GetDisplacement() * score.score.GetDistanceBetweenStaveLines() + TopMargin();
    }
}
